﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.BizFacade
{
    using System;
    using System.Data;

    using Boiling.Common;
    using Boiling.DataAccess;
    
    
    public class ActivityManager
    {


        /// <summary>
        /// 插入一个活动
        /// </summary>
        /// <param name="actdata"></param>
        /// <returns></returns>
        public bool InsertAct(b_Activitydata actdata)
        {
            return (new b_Activity()).insertact(actdata);
        }

        
        /// <summary>
        /// 用活动编号返回活动数据
        /// </summary>
        /// <param name="actid">活动编号</param>
        /// <returns>若不存在返回空</returns>
        public b_Activitydata GetActbyNo(int actid)
        {
            return (new b_Activity()).getactbyNO(actid);
        }


        /// <summary>
        /// 判断给定的活动编号是否存在对应的活动
        /// </summary>
        /// <param name="actid">活动编号</param>
        /// <returns>存在返回true，不存在返回false</returns>
        public bool JudgeActbyNo(int actid)
        {
            return (new b_Activity()).judgeactbyNO(actid);
        }



        /// <summary>
        /// 输入一个用户注册邮箱，返回其发起的活动的列表
        /// </summary>
        /// <param name="hostID">用户id</param>
        /// <returns>活动列表</returns>
        public b_Activitydata GetActbyUserID(String hostID)
        {
            return (new b_Activity()).getactbyfounder(hostID);
        }



        /// <summary>
        /// 判断某用户邮箱代表的用户是否发起了活动，要求输入用户注册邮箱
        /// </summary>
        /// <param name="hostID">用户id</param>
        /// <returns>是否发起活动</returns>
        public bool JudgeActbyUserID(String hostID)
        {
            return (new b_Activity()).judgeactbyfounder(hostID);
        }


        /// <summary>
        /// 统计一个用户究竟发起了多少活动，要求输入用户注册邮箱
        /// </summary>
        /// <param name="hostID">用户id</param>
        /// <returns>用户发起的活动数</returns>
        public int CountActbyUserID(String hostID)
        {
            return (new b_Activity()).countactbyfounder(hostID);
        }


        /// <summary>
        /// 输入一个娱乐地点的编号，输入与其有关联的所有活动
        /// </summary>
        /// <param name="amuid">娱乐地点编号</param>
        /// <returns>活动列表</returns>
        public b_Activitydata GetActbyAmuid(int amuid)
        {
            return (new b_Activity()).getactbyamuid(amuid);
        }



        /// <summary>
        /// 输入一个娱乐地点的编号，输出与其相关的所有活动的个数
        /// </summary>
        /// <param name="amuid"></param>
        /// <returns></returns>
        public int CountActbyAmuid(int amuid)
        {
            return (new b_Activity()).countactbyamuid(amuid);
        }


/*
        public bool modifyact(b_Activitydata tempdata)
        {
            //输入一个活动数据，修改与其编号相同的活动的内容为输入数据的内容
            //如果存在编号相同的活动并且修改成功，返回true,否则返回false
            int actid = Int32.Parse(tempdata.Tables["b_Activity"].Rows[0]["Act_ID"].ToString());
            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");
            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = 0; i < tempdata.Tables["b_Activity"].Columns.Count; i++)
                    newdata.Tables["b_Activity"].Rows[0][i] = tempdata.Tables["b_Activity"].Rows[0][i];
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool deleteactbyNO(int actid)
        {
            //输入一个活动的编号
            //如果存在对应编号的活动，则删除之，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Activity"].Rows[0].Delete();
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool deleteactbyamuid(int amuid)
        {
            //输入一个娱乐地点的编号
            //如果存在对应编号的娱乐地点相关的活动，则从数据库中删除对应信息，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Amu_ID =" + amuid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Activity"].Rows.Count - 1; i >= 0; i--)
                {
                    newdata.Tables["b_Activity"].Rows[i].Delete();
                }
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }
        public bool deleteactbyusername(string username)
        {
            //输入一个用户注册邮箱
            //如果存在该用户发起的活动，则删除之，并返回true
            //否则返回false
            b_Activitydata newdata = new b_Activitydata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_Founder ='" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");

            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                for (int i = newdata.Tables["b_Activity"].Rows.Count - 1; i >= 0; i--)
                {
                    newdata.Tables["b_Activity"].Rows[i].Delete();
                }
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }

        public bool markact(int actid, int newmark)
        {
            //输入活动编号，输入新打的分
            //如果存在编号对应活动，则评分人数+1，分数重新加权平均，返回true
            //否则返回false

            b_Page_Mesdata newdata = new b_Page_Mesdata();
            string selectstring = "SELECT * FROM b_Activity WHERE Act_ID =" + actid.ToString();
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Activity");
            if (newdata.Tables["b_Activity"].Rows.Count == 0)
                return (false);
            else
            {
                int i = Int32.Parse(newdata.Tables["b_Activity"].Rows[0]["Act_Marks"].ToString());
                int j = Int32.Parse(newdata.Tables["b_Activity"].Rows[0]["Act_Marks_NO"].ToString());
                newdata.Tables["b_Activity"].Rows[0]["Act_Marks"] = (i * j + newmark) / (j + 1);
                newdata.Tables["b_Activity"].Rows[0]["Act_Marks_NO"] = j + 1;
                dscommand.Update(newdata, "b_Activity");
                tempconn.Close();
                return (true);
            }
        }
        */
    }

}
